/* 
  统一发送请求的接口文件
*/
import jsonp from 'jsonp'
import { message } from 'antd'
import { api } from './api'

// 登录接口
export const reqLogin = (params = {}) => api("post", "/api/login", params);

// 请求天气对象的接口
export const reqWeather = (city) => {
  return new Promise((resolve, reject) => {
    // const url = `http://api.map.baidu.com/telematics/v3/weather?location=${city}&output=JSON&ak=Lot0blZ8Qy3xYt9X4X3cPf9BdhOtqmdN`;
    // const url = `http://api.map.baidu.com/weather/v1/?district_id=222405&data_type=all&ak=Lot0blZ8Qy3xYt9X4X3cPf9BdhOtqmdN`;
    const url = `https://restapi.amap.com/v3/weather/weatherInfo?city=${city}&output=JSON&extensions=base&key=e00d084aba1cafce3bc37a173d28d159`;
    // 发送jsonp请求
    jsonp(url, {}, (err, data) => {
      // console.log('jsonp()', err, data);
      // 如果成功了
      if (!err && data.status === '1') {
        // 取出需要的数据
        const { weather } = data.lives[0];
        resolve({  weather });
      } else {
        // 如果失败了
        message.error('获取天气信息失败!');
      }
    })
  })
}

// 获取一级/二级分类的列表
export const reqCategorys = (parentId) => api("get",'/api/manage/category/list', {parentId})

// 添加分类
export const reqAddCategory = (categoryName, parentId) => api("post",'/api/manage/category/add', {categoryName, parentId})

// 更新分类
export const reqUpdateCategory = ({categoryId, categoryName}) => api("post",'/api/manage/category/update', {categoryId, categoryName})

// 获取一个分类
export const reqCategory = (categoryId) => api("get",'/api/manage/category/info', {categoryId})

// 获取商品分页列表
export const reqProducts = (pageNum, pageSize) => api("get",'/api/manage/product/list', {pageNum, pageSize})

// 更新商品的状态(上架/下架)
export const reqUpdateStatus = (productId, status) => api("post",'/api/manage/product/updateStatus', {productId, status})

/*
搜索商品分页列表 (根据商品名称/商品描述)
searchType: 搜索的类型, productName/productDesc
 */
export const reqSearchProducts = ({pageNum, pageSize, searchName, searchType}) => api("get",'/api/manage/product/search', {
  pageNum,
  pageSize,
  [searchType]: searchName,
})

// 搜索商品分页列表 (根据商品描述)
/*export const reqSearchProducts2 = ({pageNum, pageSize, searchName}) => api("get",'/api/manage/product/search', {
  pageNum,
  pageSize,
  productDesc: searchName,
})*/

// 删除指定名称的图片
export const reqDeleteImg = (name) => api("post",'/api/manage/img/delete', {name})

// 添加/修改商品
export const reqAddOrUpdateProduct = (product) => api("post",'/api/manage/product/' + ( product._id?'update':'add'), product)
// 修改商品
// export const reqUpdateProduct = (product) => api("post",'/api/manage/product/update', product)


// 获取所有角色的列表
export const reqRoles = () => api("get",'/api/manage/role/list')
// 添加角色
export const reqAddRole = (roleName) => api("post",'/api/manage/role/add', {roleName})
// 添加角色
export const reqUpdateRole = (role) => api("post",'/api/manage/role/update', role)


// 获取所有用户的列表
export const reqUsers = () => api("get",'/api/manage/user/list')
// 删除指定用户
export const reqDeleteUser = (userId) => api("post",'/api/manage/user/delete', {userId})
// 添加/更新用户
export const reqAddOrUpdateUser = (user) => api("post",'/api/manage/user/'+(user._id ? 'update' : 'add'), user)